package cn.wl.gulimall.member.service.impl;

import cn.wl.gulimall.member.dao.MemberLevelDao;
import cn.wl.gulimall.member.entity.MemberLevelEntity;
import cn.wl.gulimall.member.exception.PhoneExsitException;
import cn.wl.gulimall.member.exception.UserNameExistException;
import cn.wl.gulimall.member.vo.MemberLoginVo;
import cn.wl.gulimall.member.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wl.common.utils.PageUtils;
import cn.wl.common.utils.Query;

import cn.wl.gulimall.member.dao.MemberDao;
import cn.wl.gulimall.member.entity.MemberEntity;
import cn.wl.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelDao memberLevelDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        MemberEntity entity = new MemberEntity();
        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        entity.setLevelId(levelEntity.getId());
        //检查用户名和手机号是否唯一,为了让controller能感知异常，使用异常机制
        checkPhoneUnique(vo.getPhone());
        checkUserName(vo.getUserName());

        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());
        //密码加密存储
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassWord());
        entity.setPassword(encode);
        this.save(entity);
    }

    @Override
    public void checkPhoneUnique(String email) throws PhoneExsitException {
        int mobile = this.count(new QueryWrapper<MemberEntity>().eq("mobile", email));
        if(mobile>0)
        throw new PhoneExsitException();
    }

    @Override
    public void checkUserName(String userName) throws UserNameExistException{
        int username = this.count(new QueryWrapper<MemberEntity>().eq("username", userName));
        if(username>0)
        throw  new UserNameExistException();
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginacct = vo.getLoginacct();
        String passWord = vo.getPassWord();
        //1.去数据库查询
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct));
        if(memberEntity==null){
            //登录失败
            return null;
        }else{
            //获取到数据库的password
            String password = memberEntity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            //2.密码匹配
            boolean matches = passwordEncoder.matches(passWord, password);
            if(matches){
                return memberEntity;
            }else{
                return null;
            }
        }
    }

}